package com.zengshuai.miniprogrambackend.Controller;

import com.zengshuai.miniprogrambackend.Entity.Address;
import com.zengshuai.miniprogrambackend.Entity.User;
import com.zengshuai.miniprogrambackend.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    //保存openid并注册用户
    @GetMapping("/saveOpenid/{openid}")
    public void saveOpenid(@PathVariable("openid") String openid) {

        if (userMapper.isExisted(openid) == 0) {
            userMapper.saveUserOpenid(openid);

        } else {
            System.out.println("用户"+openid+"已存在");


        }
    }

    @GetMapping("/getAddressListByOpenId/{openid}")
    public List<Address> getAddressListByOpenId(@PathVariable("openid") String openid) {

        userMapper.getAddress(openid);


        return userMapper.getAddress(openid);


    }

    @GetMapping("/addWxAddress/{openid}")
    public void addWxAddress(@PathVariable("openid") String openid, String username, String telnumber, String provincename, String cityname, String countyname, String detailinfo) {

        userMapper.addWxAddress(openid, username, telnumber, provincename, cityname, countyname, detailinfo);
    }

    @GetMapping("/addnewAddress")
    public void addnewAddress(String openid, String username, String telnumber, String provincename, String cityname, String countyname, String detailinfo, String isdefault) {

        userMapper.updateisdefault(openid);
        userMapper.addnewAddress(openid, username, telnumber, provincename, cityname, countyname, detailinfo, isdefault);
    }


    @GetMapping("/UpdateAddress")
    public void UpdateAddress(String id, String openid, String username, String telnumber, String provincename, String cityname, String countyname, String detailinfo, String isdefault) {

        userMapper.updateisdefault(openid);

        userMapper.UpdateAddress(id, openid, username, telnumber, provincename, cityname, countyname, detailinfo, isdefault);
    }

    @GetMapping("/deleteaddress/{id}")
    public void deleteaddress(@PathVariable("id") String id) {
        System.out.println(id);
        userMapper.deleteaddress(id);

    }

    @GetMapping("/getUserheadImageurl")
    public User getUserheadImageurl(String openid) {


        return userMapper.getUserheadImageurl(openid);
    }


}
